#include "global.h"
#include "diskio.h"
#include "interrupt.h"
#include "led.h"
#include "stm32f10x.h"                  
#include "rs232_interface.h"
#include "keyboard.h"

volatile unsigned char touch_flag = 0;

struct SysTickTimers_s
{
    uint32_t Seconds;
    volatile uint16_t Timer;
    uint16_t LedTimer;
    uint16_t MS1000;
    uint8_t KbdTimer;
    uint8_t ExtruderTimer;
} SysTickTimers;


uint16_t GetKbdTimer(void)
{
    return SysTickTimers.KbdTimer;
}

void SetKbdTimer(uint16_t msec)
{
    SysTickTimers.KbdTimer = msec;
}

uint32_t Seconds(void)
{
    return SysTickTimers.Seconds;
}

struct SysTickTimers_s* vars = &SysTickTimers;

void SysTick_Handler(void)
{
}


uint32_t TIM6_IRQCNT = 0;

void TIM6_IRQHandler(void)		//1ms中断一次
{
    if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET)     //if(TIM6->SR & TIM_IT_Update)		//
    {
        TIM_ClearITPendingBit(TIM6, TIM_FLAG_Update);   //清除中断标志   TIM6->SR = ~TIM_FLAG_Update;//
        TIM6_IRQCNT++;
        if (TIM6_IRQCNT % 200 == 0)
        {
            led_on(LED_0);
            led_off(LED_1);
            led_on(LED_2);
        }
        if (TIM6_IRQCNT % 400 == 0)
        {
            led_off(LED_0);
            led_on(LED_1);
            led_off(LED_2);
        }
        if (TIM6_IRQCNT % 500 == 0)
        {
            //            showStatusString();
        }
        if (TIM6_IRQCNT % 1000 == 0)
        {
            vars->Seconds++;
        }
        if (vars->Timer != 0)
        {
            vars->Timer--;
        }

        vars->KbdTimer++;
        if (vars->KbdTimer >= 12)
        {
            vars->KbdTimer = 0;
            KeyScan();
            //            PrintKeyStatu();
            // DBG_TRACE("bsp_KeyScan");
        }

        vars->ExtruderTimer++;
        if (vars->ExtruderTimer >= 20)
        {
            vars->ExtruderTimer = 0;
            //            extrudT_proc();
        }
    }
}

void M0_TIM_IRQHandler(void)
{
    stepm_proc(0);
}

void M1_TIM_IRQHandler(void)
{
    stepm_proc(1);
}
void M2_TIM_IRQHandler(void)
{
    stepm_proc(2);
}

void M3_TIM_IRQHandler(void)
{
    stepm_proc(3);
}

void RS232_IRQHandler(void)
{
    rs232_proc();
}

/***************************************************
 *	GPIO中断处理
 ***************************************************/
void EXTI9_5_IRQHandler(void)
{
    if (EXTI_GetITStatus(EXTI_Line5) != RESET)
    {
        GPIO_ResetBits(GPIOC, GPIO_Pin_5);

        touch_flag = 1;
        DBG_DEBUG("touch_flag");
        EXTI_ClearITPendingBit(EXTI_Line5);
    }
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number */

    DBG("Wrong parameter value detected on\r\n");
    DBG("       file  %s\r\n", file);
    DBG("       line  %d\r\n", line);

    /* Infinite loop */
    /* while (1)
    {
    } */
}

#endif
